﻿using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UITrackMissionRender : MonoBehaviour
{
    public static UITrackMissionRender instance;

    public MissionInfo missionInfo;

    private int finishNum;

    public Image panelIcon;
    public Image finishIcon;

    public TextMeshProUGUI missionName;
    public TextMeshProUGUI taskListText;
    public TextMeshProUGUI progress;

    public bool isTrack;
    private void Awake()
    {
        instance = this;
    }
    private void OnDestroy()
    {
        instance = null;
    }
    // Use this for initialization
    void Start()
    {
        finishIcon.enabled = false;
        gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if (GameData.isMissionProgressChanged)
        {
            Refresh();
            GameData.isMissionProgressChanged = false;  
        }

        //if (missionInfo.type == 1)
        //{
        //    if (GameData.isMissionProgressChanged)
        //    {
        //        Refresh();
        //        GameData.isMissionProgressChanged = false;
        //    }
        //}
        //else if (missionInfo.type == 2)
        //{
        //    if (GameData.isItemChanged)
        //    {
        //        Refresh();
        //        GameData.isItemChanged = false;
        //    }
        //}
        //else if (missionInfo.type == 3)
        //{
        //    if (GameData.isDungeonChange)
        //    {
        //        Refresh();
        //        GameData.isDungeonChange = false;
        //    }
        //}
    }

    public void TrackMission()
    {
        gameObject.SetActive(isTrack);
        if (isTrack)
        {
            ShowMission();
            Refresh();
        }
    }

    private void ShowMission()
    {
        finishNum = GameData.GetMissionProgress(missionInfo.id);

        missionName.text = missionInfo.name;

        if (missionInfo.type == 1)
        {
            RoleInfo target = StaticData.GetRoleInfo(missionInfo.value1);
            taskListText.text = $"干掉{missionInfo.num1}只{target.name}";
        }
        else if (missionInfo.type == 2)
        {
            ItemInfo target = StaticData.GetItemInfo(missionInfo.value1);
            taskListText.text = $"找到{missionInfo.num1}个{target.name}";
        }
        else if (missionInfo.type == 3)
        {
            taskListText.text = $"前往地牢";
        }

        progress.text = finishNum + " / " + missionInfo.num1;
    }

    public void Refresh()
    {
        finishNum = GameData.GetMissionProgress(missionInfo.id);
        progress.text = finishNum + " / " + missionInfo.num1;
        if (finishNum >= missionInfo.num1)
        {
            panelIcon.color = Color.green;
            finishIcon.enabled = true;
        }
        else
        {
            panelIcon.color = Color.yellow;
            finishIcon.enabled = false;
        }
    }



}
